package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import pojo.Product;
import util.DBUtil;
import dao.ProductDao;

public class ProductDaoImpl implements ProductDao {

	Connection conn;
	PreparedStatement ps;



	private Connection getConnection() {
		try {
			InitialContext cxt = new InitialContext();// / JNDI 树
			DataSource ds = (DataSource) cxt
					.lookup("java:/comp/env/jdbc/myoracle");// // 查找 [tomcat
															// 协议]/jdbc/xxxx
			Connection conn = ds.getConnection();
			// System.out.println(conn);
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
//		return DBUtil.createConnection();
	}

	@Override
	public String addProduct(Product p) {
		conn = getConnection();
		String sql = "insert into product(id,name,price,pcount,pd,wid) values(?,?,?,?,?,?)";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			if(p.getId()==null){
				p.setId(UUID.randomUUID().toString());
			}
			
			ps.setString(1, p.getId());
			ps.setString(2, p.getName());
			ps.setDouble(3, p.getPrice());
			
			ps.setInt(4, p.getCount());
			ps.setDate(5, new java.sql.Date(p.getPd().getTime()));
			ps.setString(6, p.getWid());
			int m = ps.executeUpdate();
			conn.commit();
			return p.getId();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return null;
		}
	}

	@Override
	public int deleteProduct(String pid) {
		conn = getConnection();
		String sql = "delete product where id=?";
		try {
			ps = conn.prepareStatement(sql);

			ps.setString(1,pid);
			int m = ps.executeUpdate();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public List<Product> findAllProduct(String wid) {
		conn = getConnection();
		List<Product> list = new ArrayList<Product>();
		String sql = "select * from product where wid=?";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, wid);
			ResultSet set = ps.executeQuery();
			while (set.next()) {
				Product order = new Product();
				order.setId(set.getString("id"));
				order.setName(set.getString("name"));
				order.setPrice(set.getDouble("price"));
				order.setCount(set.getInt("pcount"));
				order.setPd(new Date(set.getDate("pd").getTime()));
				order.setWid(set.getString("wid"));
				list.add(order);
			}
			conn.commit();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return list;
		}
	}
	
	@Override
	public List<Product> findAllProduct() {
		conn = getConnection();
		List<Product> list = new ArrayList<Product>();
		String sql = "select * from product";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ResultSet set = ps.executeQuery();
			while (set.next()) {
				Product order = new Product();
				order.setId(set.getString("id"));
				order.setName(set.getString("name"));
				order.setPrice(set.getDouble("price"));
				order.setCount(set.getInt("pcount"));
				order.setWid(set.getString("wid"));
				order.setPd(new Date(set.getDate("pd").getTime()));
				list.add(order);
			}
			conn.commit();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return list;
		}
	}
	

	@Override
	public int updateProduct(Product p) {
		conn = getConnection();
		String sql = "update product set name=?,price=?,pcount=?,pd=?,wid=? where id=?";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, p.getName());
			ps.setDouble(2, p.getPrice());
			ps.setInt(3, p.getCount());
			ps.setDate(4, new java.sql.Date(p.getPd().getTime()));
			ps.setString(5, p.getWid());
			ps.setString(6, p.getId());
			int m = ps.executeUpdate();
			conn.commit();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return 0;
		}
	}

	@Override
	public Product searchProdut(String pid) {
		conn = getConnection();
		String sql = "select * from product where id=?";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, pid);
			ResultSet set = ps.executeQuery();
			if (set.next()) {
				Product order = new Product();
				order.setId(set.getString("id"));
				order.setName(set.getString("name"));
				order.setPrice(set.getDouble("price"));
				order.setCount(set.getInt("pcount"));
				order.setWid(set.getString("wid"));
				order.setPd(new Date(set.getDate("pd").getTime()));
				conn.commit();
				return order;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return null;
		}
		return null;
	}
	
}
